package com.xjscrm.console.chat.mq;

import com.alibaba.fastjson.JSON;
import com.uzai.mobile.collect.api.dto.MicaTransDto;
import com.uzai.mobile.collect.api.enums.MicaMsgType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author mingyang.chen create in 2024/3/11 18:14
 */
public interface MsgHandler<T> {

    Logger log = LoggerFactory.getLogger(MsgHandler.class);

    ThreadLocal<MicaTransDto> baseCache = new ThreadLocal<>();

    /**
     * 消息类型
     */
    MicaMsgType msgType();

    Class<T> msgClass();

    /**
     * 处理消息
     */
    default void handle(MicaTransDto msg) {
        try {
            baseCache.set(msg);
            Object data = msg.getData();
            T t = JSON.parseObject(data.toString(), msgClass());
            this.doHandle(t);
        } catch (Exception e) {
            log.error("消息解析失败", e);
        } finally {
            baseCache.remove();
        }
    }

    void doHandle(T t);
}
